package org.lq.ht.view.Supplier;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.lq.ht.entity.Supplier;
import org.lq.ht.service.SupplierService;
import org.lq.ht.service.impl.SupplierServiceImpl;
import org.lq.ht.util.CastUtil;

import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.List;
import java.awt.event.ActionEvent;

public class SupplierMain extends JFrame {

	private JPanel contentPane;
	private JTextField text_serach;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SupplierMain frame = new SupplierMain();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SupplierMain() {
		
		setTitle("供应商管理");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 666, 428);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "\u6570\u636E\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel.setToolTipText("数据管理");
		panel.setBounds(10, 10, 632, 133);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("名称:");
		lblNewLabel.setBounds(173, 30, 58, 15);
		panel.add(lblNewLabel);
		
		text_serach = new JTextField();
		text_serach.setBounds(225, 27, 97, 21);
		panel.add(text_serach);
		text_serach.setColumns(10);
		
		JButton btnNewButton = new JButton("查询");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name = text_serach.getText();
				SupplierService sp = new SupplierServiceImpl();
				List<Supplier> likeSupplierByName = sp.likeSupplierByName(name);
				initTable(likeSupplierByName);
			}
		});
		btnNewButton.setBounds(362, 26, 73, 23);
		panel.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("添加");
		btnNewButton_1.setForeground(Color.BLACK);
		btnNewButton_1.setBackground(Color.WHITE);
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SupplierAdd supplierAdd = new SupplierAdd();
				supplierAdd.setVisible(true);
				supplierAdd.addWindowListener(new WindowAdapter() {
					@Override
					public void windowClosed(WindowEvent e) {
						initTable();//当关闭的时候初始化表格
					}
				});
			}
		});
		btnNewButton_1.setBounds(108, 82, 73, 23);
		panel.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("修改");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = table.getSelectedRow();
				if (index >=0 ) {
					//拿到选中行的第0行(序号)
					int no = CastUtil.castInt(table.getValueAt(index,0));
					SupplierAdd add = new SupplierAdd(no,true);
					add.setBounds(SupplierMain.this.getX(), SupplierMain.this.getY(), add.getWidth(), add.getHeight());
					add.setVisible(true);
					add.addWindowListener(new WindowAdapter() {
						public void windowClosed(WindowEvent e) {
							initTable();
						};
					});
				}else {
					JOptionPane.showConfirmDialog(SupplierMain.this, "请选择供应商");
				}
				
			}
		});
		btnNewButton_2.setBounds(234, 82, 73, 23);
		panel.add(btnNewButton_2);
		
		JButton btnNewButton_3 = new JButton("删除");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = table.getSelectedRow();
				if(index>=0) {
					//拿到按键下标  0确定  1取消
					int n =JOptionPane.showConfirmDialog(SupplierMain.this, "确定删除"+table.getValueAt(index, 1)+"供应商吗?","删除",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
					if(n==0) {
						int no = CastUtil.castInt(table.getValueAt(index, 0));
						if(new SupplierServiceImpl().deleteSupplierById(no)) {
							JOptionPane.showMessageDialog(SupplierMain.this, "删除供应商成功!");
							initTable();
						}else {
							JOptionPane.showMessageDialog(SupplierMain.this, "删除供应商失败!");
						}
					}
				}else {
					JOptionPane.showMessageDialog(SupplierMain.this, "请选择供应商!");
				}
			}
		});
		btnNewButton_3.setBounds(362, 82, 73, 23);
		panel.add(btnNewButton_3);
		
		JButton btnNewButton_4 = new JButton("详细信息");
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//拿到选中的下标
				int index = table.getSelectedRow();
				if(index>=0) {
					int id = CastUtil.castInt(table.getValueAt(index, 0));
					SupplierAdd add = new SupplierAdd(id,false);
					add.setBounds(SupplierMain.this.getX()+50, SupplierMain.this.getY()+20, add.getWidth(), add.getHeight());
					add.setVisible(true);
				}else {
					JOptionPane.showMessageDialog(SupplierMain.this, "请选择供应商!");
				}
				
			}
		});
		btnNewButton_4.setBounds(495, 82, 97, 23);
		panel.add(btnNewButton_4);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 153, 632, 228);
		contentPane.add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u7F16\u53F7", "\u540D\u79F0", "\u624B\u673A\u53F7", "\u5730\u5740", "\u516C\u53F8\u540D\u79F0"
			}
		));
		scrollPane.setViewportView(table);
		initTable();
	}
	
	
	
	/**
	 * 初始化表格
	 */
	public void initTable() {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		int rowCount = dtm.getRowCount();
		for (int i = 0; i < rowCount; i++) {
			dtm.removeRow(0);
		}
		SupplierService ss = new SupplierServiceImpl();
		List<Supplier> list = ss.findSupplierAll();
		for (Supplier s : list) {
			dtm.addRow(new Object[] {
					s.getNo(),
					s.getName(),
					s.getPhone(),
					s.getAddress(),
					s.getCompany_name()
			});
		}
	}
	/**
	 * 初始化表格(带参数)
	 */
	public void initTable(List<Supplier> list) {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		int rowCount = dtm.getRowCount();
		for (int i = 0; i < rowCount; i++) {
			dtm.removeRow(0);
		}
		for (Supplier s : list) {
			dtm.addRow(new Object[] {
					s.getNo(),
					s.getName(),
					s.getPhone(),
					s.getAddress(),
					s.getCompany_name()
			});
		}
	}
//	/**
//	 * 删除表格
//	 */
//	public void dropTable() {
//		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
//		int rowCount = dtm.getRowCount();
//		for (int i = 0; i < rowCount; i++) {
//			dtm.removeRow(0);
//		}
//	}
}
